import Http from '../http';
import type { 
  User, 
  UserQueryParams, 
  UserListResponse, 
  CreateUserRequest, 
  UpdateUserRequest,
  UserAddress,
  CreateUserAddressRequest,
  UpdateUserAddressRequest,
  UserStatsResponse
} from '@/types/user';

// 认证相关API - 对接真实后端接口
export const login = function(loginForm: any) {
    return Http.post('/api/auth/login', loginForm)
}

export const menu = function() {
    return Http.get('/api/auth/menu') 
}

export const permission = function() {
    return Http.get('/api/auth/permission') 
}

/**
 * 用户管理API
 */
export class UserApi {
  /**
   * 测试地址API连接
   */
  static testAddressApi(): Promise<any> {
    return Http.get('/api/addresses/test');
  }
  /**
   * 获取用户列表
   */
  static getUserList(params: UserQueryParams): Promise<UserListResponse> {
    return Http.get('/api/users', { params });
  }

  /**
   * 获取用户详情
   */
  static getUserById(id: number): Promise<User> {
    return Http.get(`/api/users/${id}`);
  }

  /**
   * 创建用户
   */
  static createUser(data: CreateUserRequest): Promise<User> {
    return Http.post('/api/users', data);
  }

  /**
   * 更新用户
   */
  static updateUser(id: number, data: UpdateUserRequest): Promise<User> {
    return Http.put(`/api/users/${id}`, data);
  }

  /**
   * 删除用户
   */
  static deleteUser(id: number): Promise<void> {
    return Http.delete(`/api/users/${id}`);
  }

  /**
   * 批量删除用户
   */
  static batchDeleteUsers(ids: number[]): Promise<void> {
    return Http.post('/api/users/batch-delete', { ids });
  }

  /**
   * 更新用户状态
   */
  static updateUserStatus(id: number, status: 0 | 1): Promise<User> {
    return Http.patch(`/api/users/${id}/status`, { status });
  }

  /**
   * 获取用户统计信息
   */
  static getUserStats(): Promise<UserStatsResponse> {
    return Http.get('/api/users/stats');
  }

  /**
   * 获取用户地址列表
   */
  static getUserAddresses(userId: number): Promise<UserAddress[]> {
    return Http.get(`/api/users/${userId}/addresses`);
  }

  /**
   * 创建用户地址
   */
  static createUserAddress(userId: number, data: CreateUserAddressRequest): Promise<UserAddress> {
    return Http.post(`/api/users/${userId}/addresses`, data);
  }

  /**
   * 更新用户地址
   */
  static updateUserAddress(userId: number, addressId: number, data: UpdateUserAddressRequest): Promise<UserAddress> {
    return Http.put(`/api/users/${userId}/addresses/${addressId}`, data);
  }

  /**
   * 删除用户地址
   */
  static deleteUserAddress(userId: number, addressId: number): Promise<void> {
    return Http.delete(`/api/users/${userId}/addresses/${addressId}`);
  }

  /**
   * 设置默认地址
   */
  static setDefaultAddress(userId: number, addressId: number): Promise<UserAddress> {
    return Http.patch(`/api/users/${userId}/addresses/${addressId}/default`);
  }

  /**
   * 获取所有用户地址列表（管理员）
   */
  static getAllAddresses(params: any): Promise<any> {
    return Http.get('/api/addresses', { params });
  }

}